package com.lvy.hczn.front.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.business.mapper.TransportMapper;
import com.lvy.hczn.front.business.service.TransportDetailGoodsService;
import com.lvy.hczn.front.business.service.TransportDetailService;
import com.lvy.hczn.front.business.service.TransportService;
import com.lvy.hczn.front.system.domain.business.Transport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class TransportServiceImpl extends ServiceImpl<TransportMapper, Transport> implements TransportService {

    @Autowired
    private TransportMapper transportMapper;

    @Autowired
    private TransportDetailService transportDetailService;

    @Autowired
    private TransportDetailGoodsService transportDetailGoodsService;

    @Override
    public void syncFromTransmit(Transport transport) {
        Transport transportInfo = super.getById(transport.getId());
        if (transportInfo == null) {
            super.save(transport);
        } else {
            super.updateById(transport);
        }
        if (transport.getDetailList() != null && !transport.getDetailList().isEmpty()) {
            transportDetailService.saveOrUpdateBatch(transport.getDetailList());
            transport.getDetailList().forEach(detail -> {
                if (detail.getCartons() != null && !detail.getCartons().isEmpty()) {
                    transportDetailGoodsService.saveOrUpdateBatch(detail.getCartons());
                }
            });
        }
    }
}
